package org.vectrics.recruiter.reporting;



import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.vectrics.VectricsException;
import org.vectrics.common.reporting.pdf.BaseReport;
import org.vectrics.common.reporting.pdf.LogoWriter;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;

import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;


abstract public class RecruitBaseReport extends BaseReport {
	private transient static Logger log = Logger.getLogger(RecruitBaseReport.class);
    
	private PdfTemplate footerTemplate = null; 
	private PdfTemplate logoTemplate = null; 
    private boolean activeOnly = true;


	private int COMPANY_FONT_SIZE = 20;
	private BaseFont COMPANY_FONT = null;
	private Color COMPANY_FONT_COLOR = new Color(200, 200, 180);
	private Color COMPANY_FONT_COLOR_2 = new Color(0, 0, 130);
    private int FOOTER_FONT_SIZE = 8;
	private BaseFont FOOTER_FONT = null;
	private Color FOOTER_FONT_COLOR = new Color(100, 100, 150);
	private Domain domain = null;

	private float footerSpace = 15;	
	public static Color HEADER_COLOR = new Color(240, 240, 210);
	public static Color BACKGROUND_DIAMOND_COLOR = new Color(230, 230, 205);	

	
	public RecruitBaseReport(Domain domain, Locale locale) {
		super(locale);
		this.domain = domain;
		
		try {
			FOOTER_FONT = 
				BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
			COMPANY_FONT = 
				BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
			setBottomMargin(70);
		}
		catch (Exception de) {
			log.error("Exception creating footer font", de);
		}
	}

	protected Domain getDomain() {
		return(this.domain);
	}
	
	protected Rectangle getPageSize() {
		return(PageSize.A4);
	}
	
	protected void onDocumentStart(Document document) throws VectricsException {
		log.debug("onDocumentStart()");
		try {
			if (log.isDebugEnabled()) {
				log.debug("onDocumentStart() - document.top = " + document.top());
				log.debug("onDocumentStart() - document.bottom = " + document.bottom());
				log.debug("onDocumentStart() - document.left = " + document.left());
				log.debug("onDocumentStart() - document.right = " + document.right());
	
				log.debug("onDocumentStart() - document.topMargin = " + document.topMargin());
				log.debug("onDocumentStart() - document.bottomMargin = " + document.bottomMargin());
			}

			float logoHeight = 20;
			float headerWidth = document.right() - document.leftMargin();// - (document.rightMargin() + document.leftMargin());
			float headerHeight = (12 * logoHeight) / 10;
			logoTemplate = this.getDirectContent().createTemplate(
					headerWidth, headerHeight);
					
					
			Graphics2D g2 = logoTemplate.createGraphics(headerWidth, headerHeight);					
			g2.setPaint(HEADER_COLOR);
			g2.fillRect(0, 0, (int)headerWidth, (int)headerHeight);
			g2.dispose();

			LogoWriter logoWriter = new LogoWriter(g2);
			float diamondX = 50;
			float diamondY = 15;
			float diamondHeight = 40;
			float diamondWidth = 100;
			logoWriter.drawDiamond(diamondX, diamondY, diamondWidth, diamondHeight, g2, BACKGROUND_DIAMOND_COLOR);
			diamondX += 5 + diamondWidth / 2;
			diamondY += 3 + diamondHeight / 2;
			logoWriter.drawDiamond(diamondX, diamondY, diamondWidth, diamondHeight, g2, BACKGROUND_DIAMOND_COLOR);

			diamondX += 5 + diamondWidth / 2;
			diamondY -= (3 + diamondHeight / 2);
			logoWriter.drawDiamond(diamondX, diamondY, diamondWidth, diamondHeight, g2, BACKGROUND_DIAMOND_COLOR);
			
			if (RecruitConfig.getRecruitInstance().getShowVectricsLogoOnReports(domain)) {
				int logoWidth = logoWriter.getWidth(logoHeight, g2);
				logoWriter.drawLogo(headerWidth - logoWidth, 0, logoHeight, g2);
			}

			String companyName = RecruitConfig.getRecruitInstance().getCompanyName(domain);
			
			logoTemplate.beginText();			
			logoTemplate.setFontAndSize(COMPANY_FONT, COMPANY_FONT_SIZE);
			logoTemplate.setColorStroke(COMPANY_FONT_COLOR);
			logoTemplate.setColorFill(COMPANY_FONT_COLOR);
			logoTemplate.setTextMatrix(6, 4);
			logoTemplate.showText(companyName);
			logoTemplate.endText();

			logoTemplate.beginText();			
			logoTemplate.setFontAndSize(COMPANY_FONT, COMPANY_FONT_SIZE);
			logoTemplate.setColorStroke(COMPANY_FONT_COLOR_2);
			logoTemplate.setColorFill(COMPANY_FONT_COLOR_2);
			logoTemplate.setTextMatrix(4, 6);
			logoTemplate.showText(companyName);
			logoTemplate.endText();

			
			this.getDirectContent().addTemplate(logoTemplate, document.leftMargin(), 
						document.top() + logoHeight - headerHeight); 
		}
		catch (Exception ex) {
			log.error("Exception creating header image", ex);
		}
		footerTemplate = this.getDirectContent().createTemplate(document.right(), document.bottomMargin());
	} 

	
	protected void onDocumentEnd(Document document) throws VectricsException {
		log.debug("onDocumentEnd()");
		addTotalPagesToFooter(document);
	}
	 
	
	protected void onEndPage(PdfWriter writer, Document document) throws VectricsException {
		if (log.isDebugEnabled())
			log.debug("onEndPage() page number = " + document.getPageNumber());
		this.addPageNumberToFooter(document);
	}
	
		
	protected void addPageNumberToFooter(Document document) throws VectricsException {
		log.debug("addPageNumberToFooter()");
		float y = document.bottomMargin() - getFooterSpace();
		float textY = y - 10;

		log.debug("addPageNumberToFooter() - creating text which will display on footer");
		String footerText = this.getPhrase("phrase.page") + document.getPageNumber() + " " + this.getPhrase("phrase.of") + " ";

		String dateText = CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(this.getLocale()).format(new Date());

		log.debug("addPageNumberToFooter() - drawing line on bottom margin");
		this.getDirectContent().setLineWidth(ReportConstants.FOOTER_LINE_SIZE);
		this.getDirectContent().setColorStroke(ReportConstants.FOOTER_LINE_COLOR);
		this.getDirectContent().moveTo(document.left(), y);
		this.getDirectContent().lineTo(document.right(), y);
		this.getDirectContent().stroke();
					
		log.debug("addPageNumberToFooter() - drawing text below the line");
		this.getDirectContent().beginText();
		this.getDirectContent().setFontAndSize(FOOTER_FONT, FOOTER_FONT_SIZE);
		this.getDirectContent().setColorStroke(FOOTER_FONT_COLOR);
		float len = FOOTER_FONT.getWidthPoint(footerText, FOOTER_FONT_SIZE);
		float dateLength = FOOTER_FONT.getWidthPoint(dateText, FOOTER_FONT_SIZE);
		this.getDirectContent().setTextMatrix(document.leftMargin(), textY);
		this.getDirectContent().showText(footerText);
		this.getDirectContent().endText();

		this.getDirectContent().beginText();
		this.getDirectContent().setTextMatrix(document.right() - dateLength, textY);
		this.getDirectContent().showText(dateText);
		this.getDirectContent().endText();
		
		
		log.debug("addPageNumberToFooter() - adding template for this page");
		float templateX = document.leftMargin() + len;
		float templateY = textY;
		log.debug("addPageNumberToFooter() - templateX = " + templateX);
		log.debug("addPageNumberToFooter() - templateY = " + templateY);
		
		
		this.getDirectContent().addTemplate(footerTemplate, templateX, templateY);
	}

		
	/**
	 * 
	 * @param document
	 * @throws VectricsException
	 */
	protected void addTotalPagesToFooter(Document document) throws VectricsException {
		log.debug("addTotalPagesToFooter()");
		footerTemplate.beginText();
		footerTemplate.setFontAndSize(this.FOOTER_FONT, this.FOOTER_FONT_SIZE);
		footerTemplate.showText("" + this.getPdfWriter().getPageNumber());
		footerTemplate.endText(); 
	}


	protected void addTableHeaderCell(PdfPTable table, String phraseCode) {
		log.debug("addTableHeaderCell()");
		PdfPCell hdrCell = new PdfPCell(new Phrase(this.getPhrase(phraseCode), FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD)));
		hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
		table.addCell(hdrCell);	
	}
	   
	public boolean getActiveOnly() {
		return activeOnly;
	}

	public void setActiveOnly(boolean activeOnly) {
		this.activeOnly = activeOnly;
	}

	/**
	 * @return
	 */
	public float getFooterSpace() {
		return footerSpace;
	}

	/**
	 * @param footerSpace
	 */
	public void setFooterSpace(float footerSpace) {
		this.footerSpace = footerSpace;
	}
	
}
